import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class MultipleMain extends StatefulWidget {
  @override
  _MultipleMainState createState() => _MultipleMainState();
}

class _MultipleMainState extends State<MultipleMain> {
  int _currentIndex;

  //按钮点击结果
  Map<int, bool> _select = {};

  //获取所有的数据
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("mulitple"),
      ),
      body: ConstrainedBox(
        constraints: BoxConstraints.expand(),
        child: Stack(
          alignment: Alignment.topCenter,
          children: [
            Column(
              children: getContentData(),
            ),
            Positioned(
              child: Padding(
                padding: EdgeInsets.all(16),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    RaisedButton(
                      onPressed: () {},
                      child: Text(
                        "last",
                        style: TextStyle(fontSize: 36),
                      ),
                    ),
                    RaisedButton(
                      onPressed: () {},
                      child: Text(
                        "last",
                        style: TextStyle(fontSize: 36),
                      ),
                    ),
                    RaisedButton(
                      onPressed: () {},
                      child: Text(
                        "last",
                        style: TextStyle(fontSize: 36),
                      ),
                    ),
                    RaisedButton(
                      onPressed: _onTabNext,
                      child: Text(
                        "next",
                        style: TextStyle(fontSize: 36),
                      ),
                    ),
                  ],
                ),
              ),
              left: 0,
              right: 0,
              bottom: 0,
            )
          ],
        ),
      ),
    );
  }

  //返回正文内容
  List<Widget> getContentData() {
    var list = <Widget>[];
    //总记录
    list.add(Text(
      "50/100",
      style: TextStyle(fontSize: 18),
    ));
    list.add(
      Divider(color: Colors.green),
    );
    list.add(
      Text(
          "topitopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopicc"),
    );
    list.add(
      Divider(color: Colors.green),
    );

    for (int i = 0; i < 4; i++) {
      getItemAnswer(list, i);
    }

    return list;
  }

  void getItemAnswer(List<Widget> list, int index) {
    list.add(GestureDetector(
      child: Row(
        children: [
          //内容
          Checkbox(
            value: _select[index] == null ? false : _select[index],
            activeColor: Colors.red, //选中时的颜色
            onChanged: (value) {
              setSelected(index);
            },
          ),
          Expanded(
              child: Text(
                  "oneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneoneone"))
        ],
      ),
      onTap: () {
        setSelected(index);
      },
    ));
    list.add(Padding(
      padding: EdgeInsets.only(left: 16),
      child: Divider(color: Colors.green),
    ));
  }

  void _onTabNext() {
    print("_select = $_select");
  }

  void setSelected(int index) {
    setState(() {
      _select[index] = _select[index] == null ? true : !_select[index];
    });
  }
}
